package com.star.book.utils;

import com.star.book.entity.BookChapter;
import com.star.book.entity.BookInfo;
import com.star.book.service.IBookChapterService;
import com.star.book.service.IBookInfoService;

import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class GetBookInfo {



    public static void getBookInfo() throws InterruptedException {
//        String url = "http://hao123.zongheng.com/store/c1/w0/s0/p1/all.html";
        System.setProperty("webdriver.chrome.driver", "C:\\Users\\CMR\\Documents\\java\\webDriver\\chromedriver.exe");
        WebDriver webDriver = new ChromeDriver();
        webDriver.manage().window().maximize();
        webDriver.get("https://www.dingdiann.com/ddk_1/");
        List<String> bookUrl = new ArrayList<>();
        List<String> bookImgSrc = new ArrayList<>();
        List<String> bookNameList = new ArrayList<>();
        List<String> bookDescList = new ArrayList<>();
        List<String> bookAuthorList = new ArrayList<>();
        List<String> bookTypeList = new ArrayList<>();
        for (int i = 1; i <= 30; i++) {
            String href = webDriver.findElement(By.xpath("//div[@class='l']/ul/li["+i+"]/span[@class='s2']/a")).getAttribute("href");
            bookUrl.add(href);
        }

        System.out.println("书籍URL长度：" + bookUrl.size());
        for (int i = 0; i < bookUrl.size(); i++) {
            webDriver.get(bookUrl.get(i));
            String bookName = webDriver.findElement(By.xpath("//div[@id='bookdetail']/div[@id='info']/h1")).getText();
            bookNameList.add(bookName);
            String bookAuthor = webDriver.findElement(By.xpath("//div[@id='info']/p[1]")).getText();
            bookAuthorList.add(bookAuthor);
            JavascriptExecutor js = (JavascriptExecutor) webDriver;
//            Object o = js.executeScript("document.getElementById('fmimg').children");

//            String imgUrl = webDriver.findElement(By.xpath("//div[@id='fmimg']/img/@src")).getAttribute("href");

//            bookImgSrc.add(imgUrl);
            String bookDesc = webDriver.findElement(By.xpath("//div[@id='maininfo']/div[@id='bookdetail']/div[@id='intro']")).getText();
            bookDescList.add(bookDesc);
        }
        System.out.println("长度为：" + bookImgSrc.size() + bookNameList.size() + bookDescList.size());
        IBookInfoService bookInfoService = SpringUtil.getBean(IBookInfoService.class);
        for (int i = 0; i < bookNameList.size(); i++) {
            BookInfo bookInfo = new BookInfo();
//            bookInfo.setBookImgurl(bookImgSrc.get(i));
            bookInfo.setBookName(bookNameList.get(i));
            bookInfo.setBookDesc(bookDescList.get(i));
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
            bookInfo.setBookAddtime(df.format(new Date()));
            bookInfo.setBookUpdatetime(df.format(new Date()));
            bookInfo.setBookAuthor(bookAuthorList.get(i));
            bookInfo.setBookType("玄幻");

            bookInfoService.save(bookInfo);
            System.out.println("当前插入第" + i +"本了！");
        }
        List<String> bookChapterUrl = new ArrayList<>();
        List<String> bookChapterDesc = new ArrayList<>();
        List<String> bookChapterName = new ArrayList<>();
        IBookChapterService bookChapterService = (IBookChapterService) SpringUtil.getBean(IBookChapterService.class);
        for (int i = 0; i < bookUrl.size(); i++) {
                webDriver.get(bookUrl.get(i));
              for (int j = 13;j <= 113;j++){
                  String href = webDriver.findElement(By.xpath("//div[@id='list']/dl/dd["+j+"]/a")).getAttribute("href");
                  webDriver.get(href);
                  BookChapter bookChapter = new BookChapter();
                  String chapterName = webDriver.findElement(By.xpath("//div[@class='bookname']/h1")).getText();
                  String chapterDesc = webDriver.findElement(By.xpath("//div[@class='box_con']/div[@id='content']")).getText();
                  bookChapter.setBookchapterName(chapterName);
                  bookChapter.setBookchapterDesc(chapterDesc);
                  bookChapter.setBookId(i+1);
                  SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
                  bookChapter.setBookchapterUpdate(df.format(new Date()));

                  bookChapterService.save(bookChapter);
                  System.out.println("当前弄完第" + i + "本小说的" + "第" + j +"章了!");
              }
        }





    }
}
